package com.nginxmanager.comment;

import com.nginxmanager.domain.Conf;
import com.nginxmanager.domain.Proxy;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018-01-11.
 */
public class FileTool {
    //配置项查找
    public List<String> findOne(String url, Conf conf) {
        File file = new File(url+"\\conf\\nginx.conf");
        System.out.println("line mmp:"+url);
        BufferedReader reader = null;
        List<String> serverList = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getKey()+" )(.*)");
                Matcher m = p.matcher(tempString);
                Pattern p1 = Pattern.compile("(.*)(#)(.*)");
                Matcher m1 = p1.matcher(tempString);
                if (m.matches()&&!m1.matches()){
                    System.out.println("line " + line + ": " + tempString);
                    serverList.add(tempString);
                    return serverList;
                }
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return serverList;
    }
//增加配置项
    public void addOne(String str,Conf conf) {
        File file = new File(str+"\\conf\\nginx.conf");
        File fileOut = new File(str+"\\conf\\nginx123.conf");
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getConsult()+")(.*)");
                Matcher m = p.matcher(tempString);
                bw.write(tempString + "\n");
                if (m.matches()){
                    bw.write(conf.getAllConf() + ""+conf.getValue()+";"+"\n");
                    while((tempString=reader.readLine()) != null){
                        bw.write(tempString + "\n");
                    }
                    System.out.println("增加: " + conf.getAllConf());
                }
                line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        fileOut.renameTo(file);
    }

    //修改一项配置
    //通过文件读取，备份，写入，代替，完成修改代理服务器
    public void gaiOne(String str,Conf conf) {
        File file = new File(str+"\\conf\\nginx.conf");
        File fileOut = new File(str+"\\conf\\nginx123.conf");
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        List<String> serverList = new ArrayList<>();
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getKey()+" )(.*)");
                Matcher m = p.matcher(tempString);
                Pattern p1 = Pattern.compile("(.*)(#)(.*)");
                Matcher m1 = p1.matcher(tempString);
                if (m.matches()&&!m1.matches()){
                    bw.write(conf.getAllConf() + ""+conf.getValue()+";"+"\n");
                    System.out.println("修改: " + conf.getAllConf());
                    continue;
                }
                bw.write(tempString + "\n");
                line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        fileOut.renameTo(file);
    }
//删除一项配置
    public void deleteOne(String str,Conf conf) {
        File file = new File(str+"\\conf\\nginx.conf");
        File fileOut = new File(str+"\\conf\\nginx123.conf");
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getKey()+")(.*)");
                Matcher m = p.matcher(tempString);
                if (m.matches()){
                    System.out.println("删除: " + conf.getKey());
                    continue;
                }
                bw.write(tempString + "\n");
                line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        fileOut.renameTo(file);
    }

    /**
     * 删除单个文件
     *
     * @param fileName
     *            要删除的文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }

    //遍历单个文件
    public List<String> readFile(String str) {
        File file = new File(str);
        BufferedReader reader = null;
        List<String> serverList = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while((tempString=reader.readLine()) != null){
                System.out.println(tempString);
                serverList.add("第"+line+"行："+tempString+"\n");
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return serverList;
    }
    //备份清空日志文件
    public List<String> copyFile(String str,String str1) throws IOException {
        List<String> serverList = new ArrayList<>();
        File file = new File(str);
        File fileOut = new File(str1);
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                    bw.write(tempString + "\n");
                    serverList.add("第"+line+"行："+tempString+"\n");
                    line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        return serverList;
    }

}
